package io.jee.runshan.controller.admin;

import io.jee.medusa.orm.condition.Condition;
import io.jee.medusa.orm.condition.ConditionGroup;
import io.jee.medusa.orm.pager.Pager;
import io.jee.medusa.vo.ResultBean;
import io.jee.runshan.domain.PreSaleOrder;
import io.jee.runshan.service.IPreSaleOrderService;
import io.jee.runshan.utils.StringUtils;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.BeansException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/admin/preSaleOrder/")
public class PreSaleOrderController {
	@Resource
	private IPreSaleOrderService preSaleOrderService;

	@RequestMapping("list")
	public void list(Pager<PreSaleOrder> pager, Model model,
			@RequestParam(value = "key", required = false) String key, 
			@RequestParam(value = "status", required = true, defaultValue = "-1") Integer status) {
		ConditionGroup group = new ConditionGroup();
		if (status > -1) {
			group.and(Condition.onNumber("status").eq(status));
		}
		if (StringUtils.notBlank(key)) {
			ConditionGroup subGroup = new ConditionGroup();
			subGroup.and(Condition.onText("orderCode").eq(key));
			subGroup.or(Condition.onText("contact.mobile").eq(key));
			subGroup.or(Condition.onText("contact.name").like(key + "%"));
			subGroup.or(Condition.onText("alipayAccount").eq(key));
			group.and(subGroup);
		}
		pager = preSaleOrderService.list(pager, group, "desc", "orderTime");
		model.addAttribute("key", key);
		model.addAttribute("status", status);
	}
	
	@RequestMapping(value = "/ajaxDetail", produces = { "application/json" })
	@ResponseBody
	public ResultBean<PreSaleOrder> ajaxDetail(Integer orderId) {
		PreSaleOrder order = null;
		try {
			order = preSaleOrderService.get(orderId);
		} catch (BeansException e) {
			e.printStackTrace();
			return ResultBean.result(false, "操作异常，请稍候再试！");
		}
		return ResultBean.result(true, "获取订单详情成功", order);
	}
	
	@RequestMapping(value = "/ajaxSave", produces = { "application/json" })
	@ResponseBody
	public ResultBean<?> ajaxSave(Integer orderId, Integer status, String alipaySerial, String documentSerial) {
		try {
			PreSaleOrder order = preSaleOrderService.get(orderId);
			if (order == null) {
				return ResultBean.result(false, "数据错误，请刷新后再操作！");
			}
			
			if(status == 1) {
				PreSaleOrder aOrder = preSaleOrderService.unique("alipaySerial", alipaySerial);
				if (aOrder != null && aOrder.getId() != order.getId()) {
					return ResultBean.result(false, "支付宝流水号已存在，请确认后再提交！"); 
				}
				order.setPayTime(new Date());
				order.setAlipaySerial(alipaySerial);
			} else {
				PreSaleOrder dOrder = preSaleOrderService.unique("documentSerial", documentSerial);
				if (dOrder != null && dOrder.getId() != dOrder.getId()) {
					return ResultBean.result(false, "单据号已存在，请确认后再提交！"); 
				}
				order.setDocumentSerial(documentSerial);
			}
			order.setStatus(status);
			preSaleOrderService.update(order);
		} catch (BeansException e) {
			e.printStackTrace();
			return ResultBean.result(false, "操作异常，请稍候再试！");
		}
		return ResultBean.result(true, "保存成功");
	}
	
	@RequestMapping(value = "/ajaxDel", produces = { "application/json" })
	@ResponseBody
	public ResultBean<?> ajaxDel(Integer orderId) {
		try {
			PreSaleOrder order = preSaleOrderService.get(orderId);
			if (order == null) {
				return ResultBean.result(false, "数据错误，请刷新后再操作！");
			}
			preSaleOrderService.delete(order);
		} catch (BeansException e) {
			e.printStackTrace();
			return ResultBean.result(false, "操作异常，请稍候再试！");
		}
		return ResultBean.result(true, "获取废弃成功");
	}
	
}
